<template>
  <div class="login-page">
    <div class="login-background-shapes">
      <div class="shape-circle"></div>
      <div class="shape-square"></div>
      <div class="shape-triangle"></div>
    </div>
    
    <div class="login-container">
      <div class="login-header text-center mb-6">
        <h1 class="text-3xl font-bold text-white mb-3">车辆管理系统</h1>
        <p class="text-gray-200 text-lg">统一管理车辆、驾驶员和相关资源</p>
      </div>
      
      <el-card shadow="hover" class="login-card">
        <div class="login-form-header">
          <h2 class="text-xl font-semibold mb-6">用户登录</h2>
        </div>
        
        <el-form 
          ref="loginFormRef" 
          :model="loginForm" 
          :rules="loginRules" 
          label-position="top" 
          @keyup.enter="handleLogin"
        >
          <el-form-item label="用户名" prop="username">
            <el-input 
              v-model="loginForm.username" 
              placeholder="请输入用户名" 
              prefix-icon="User"
              size="large"
            />
          </el-form-item>
          
          <el-form-item label="密码" prop="password">
            <el-input 
              v-model="loginForm.password" 
              type="password" 
              placeholder="请输入密码" 
              prefix-icon="Lock"
              show-password
              size="large"
            />
          </el-form-item>
          
          <div class="flex items-center justify-between mb-4">
            <el-checkbox v-model="rememberMe">记住我</el-checkbox>
            <a href="#" class="text-primary hover:text-primary-dark">忘记密码?</a>
          </div>
          
          <el-form-item>
            <el-button 
              type="primary" 
              class="w-full login-btn" 
              :loading="loading" 
              @click="handleLogin"
              size="large"
            >
              登录系统
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="login-tips mt-4 py-3 px-4 bg-gray-50 rounded-lg">
          <p class="text-gray-500 text-sm flex items-center">
            <el-icon class="mr-2"><InfoFilled /></el-icon>
            系统默认账户: admin / admin123
          </p>
        </div>
      </el-card>
      
      <div class="footer-info text-center mt-6 text-gray-200 text-sm">
        <p>© 2023 某某企业 版权所有</p>
        <p class="mt-1">联系电话: 400-123-4567 | 技术支持: support@example.com</p>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, InfoFilled } from '@element-plus/icons-vue'
import { setToken, setUserInfo, setMockLogin } from '@/utils/auth'
import request from '@/utils/request'

export default {
  name: 'Login',
  components: {
    User,
    Lock,
    InfoFilled
  },
  setup() {
    const router = useRouter()
    const route = useRoute()
    const loginFormRef = ref(null)
    const loading = ref(false)
    const rememberMe = ref(false)
    
    const loginForm = reactive({
      username: 'admin',
      password: 'admin123'
    })
    
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码不能少于6个字符', trigger: 'blur' }
      ]
    }
    
    const handleLogin = async () => {
      loading.value = true
      
      try {
        // 首先尝试正常登录
        const response = await request.post('/auth/login', {
          username: loginForm.username,
          password: loginForm.password
        })
        
        if (response && response.code === 200 && response.data && response.data.token) {
          // 正常登录成功
          handleLoginSuccess(response.data)
        } else {
          console.log('正常登录失败，尝试模拟登录...')
          // 使用模拟登录
          const mockToken = setMockLogin()
          
          if (mockToken) {
            ElMessage({
              message: '已使用模拟账号登录',
              type: 'success'
            })
            
            // 获取重定向地址
            const redirect = route.query.redirect
            console.log('模拟登录成功，准备重定向到:', redirect || '/page')
            
            // 直接使用location.href强制跳转
            window.location.href = redirect || '/page'
          } else {
            ElMessage.error('登录失败，请联系管理员')
          }
        }
      } catch (error) {
        console.error('登录失败:', error)
        // 使用模拟登录作为备选方案
        const mockToken = setMockLogin()
        
        if (mockToken) {
          ElMessage({
            message: '已使用模拟账号登录',
            type: 'success'
          })
          
          // 获取重定向地址
          const redirect = route.query.redirect
          console.log('模拟登录成功(异常处理路径)，准备重定向到:', redirect || '/page')
          
          // 直接使用location.href强制跳转
          window.location.href = redirect || '/page'
        } else {
          ElMessage.error('登录失败，请联系管理员')
        }
      } finally {
        loading.value = false
      }
    }

    // 处理登录成功
    const handleLoginSuccess = (data) => {
      setToken(data.token)
      setUserInfo({
        id: data.userId,
        name: data.realName || data.username,
        role: data.role
      })
      
      // 记住用户名
      if (rememberMe.value) {
        localStorage.setItem('rememberedUser', loginForm.username)
      } else {
        localStorage.removeItem('rememberedUser')
      }
      
      ElMessage({
        message: '登录成功，欢迎回来！',
        type: 'success'
      })
      
      // 获取重定向地址
      const redirect = route.query.redirect
      console.log('正常登录成功，准备重定向到:', redirect || '/page')
      
      // 直接使用location.href强制跳转
      window.location.href = redirect || '/page'
    }
    
    // 加载记住的用户名
    if (localStorage.getItem('rememberedUser')) {
      loginForm.username = localStorage.getItem('rememberedUser')
      rememberMe.value = true
    }
    
    return {
      loginForm,
      loginRules,
      loginFormRef,
      loading,
      rememberMe,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-page {
  width: 100%;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0062cc 0%, #0a47a9 100%);
  padding: 20px;
  position: relative;
  overflow: hidden;
}

.login-background-shapes {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.shape-circle {
  position: absolute;
  width: 300px;
  height: 300px;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
  border-radius: 50%;
  top: 10%;
  right: 10%;
}

.shape-square {
  position: absolute;
  width: 200px;
  height: 200px;
  background: rgba(255,255,255,0.05);
  border-radius: 20px;
  bottom: 10%;
  left: 5%;
  transform: rotate(15deg);
}

.shape-triangle {
  position: absolute;
  width: 0;
  height: 0;
  border-left: 200px solid transparent;
  border-right: 200px solid transparent;
  border-bottom: 350px solid rgba(255,255,255,0.03);
  top: -10%;
  left: 50%;
  transform: translateX(-50%);
}

.login-container {
  width: 100%;
  max-width: 440px;
  position: relative;
  z-index: 1;
}

.login-card {
  border-radius: 16px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.25);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
  padding: 25px 30px;
  background-color: white;
}

.login-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}

.login-form-header {
  text-align: center;
  position: relative;
  margin-bottom: 10px;
}

.login-form-header::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 3px;
  background-color: var(--el-color-primary);
  border-radius: 3px;
}

.login-btn {
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.5px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: 8px;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 15px rgba(24, 144, 255, 0.25);
}

.login-tips {
  border-left: 3px solid var(--el-color-primary);
}

:deep(.el-input__wrapper) {
  padding: 4px 12px;
  border-radius: 8px;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.05);
}

:deep(.el-form-item__label) {
  font-weight: 500;
  padding-bottom: 8px;
}

:deep(.el-form-item) {
  margin-bottom: 22px;
}

.footer-info {
  opacity: 0.8;
  transition: opacity 0.3s;
}

.footer-info:hover {
  opacity: 1;
}

@media (max-width: 640px) {
  .login-card {
    padding: 20px;
}

  .login-container {
    max-width: 90%;
  }
}
</style> 